package com.cn.ZhiKeDianShang.presenter;

import android.content.Context;

import com.cn.ZhiKeDianShang.api.RxHelper;
import com.cn.ZhiKeDianShang.api.RxObserver;
import com.cn.ZhiKeDianShang.base.BasePresenter;
import com.cn.ZhiKeDianShang.model.request.BaseRequestEntity;
import com.cn.ZhiKeDianShang.model.request.LoginBean;
import com.cn.ZhiKeDianShang.model.response.BaseResponseEntity;
import com.cn.ZhiKeDianShang.model.response.UserInfoBean;
import com.cn.ZhiKeDianShang.presenter.view.LoginUserView;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;

/**
 * Created by chengaofu on 2017/6/27.
 */

public class LoginUserPresenter extends BasePresenter<LoginUserView> {

    private LoginUserView mLoginUserView;

    public LoginUserPresenter(Context context, LoginUserView view) {
        super(context, view);
        mLoginUserView = view;

    }

    public void onLogin(String username, String pwd) {
        RxObserver rxObserver = new RxObserver<UserInfoBean>() {
            @Override
            public void onHandleSuccess(UserInfoBean resultBean) {
                mLoginUserView.onLogin(resultBean);
            }

            @Override
            public void onHandleComplete() {

            }

            @Override
            public void onHandleFailed(String message) {
                mLoginUserView.requestFailed(message);
            }
        };
        LoginBean loginBean = new LoginBean();
        loginBean.setAccount(username);
        loginBean.setPassword(pwd);
        BaseRequestEntity<LoginBean> requestEntity =
                new BaseRequestEntity<LoginBean>();
        requestEntity.setData(loginBean);
        Observable<BaseResponseEntity<UserInfoBean>> observable = mRxSerivce.onLogin(loginBean);
        Disposable disposable = observable
                .compose(RxHelper.<UserInfoBean>handleObservableResult())
                .subscribeWith(rxObserver);
        addSubscribe(disposable);
    }


}
